// pagesEnterprise/pages/clientManage/clientManage.js
import ajax from '../../../utils/index';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabCurrent: 1,
    customList: [],
    userTeamCount: {},
    month: '',
    monthText: '',
    search: ''
  },
  /**
     * input 变化事件
     */
  inputChange(e) {
    this.data.search = e.detail.value
  },
  /**
   * 搜索事件
   */
  inputConfirm() {
    this.get_custom_list()
  },

  /**
   * TAB改变 - 时间选择
   */
  tabChange: function (e) {
    console.log(e.currentTarget.id)
    this.setData({
      tabCurrent: e.currentTarget.id
    })
    this.get_user_team_data()
  },

  /**
   * 日期选择
   */
  dateChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      month: e.detail.value,
      monthText: e.detail.value.substr(5, 5),
      tabCurrent: 6
    })
    this.get_user_team_data()
  },

  /**
   * 接口 - 获取客户列表
   */
  get_custom_list: function () {
    let that = this
    ajax.get('/wx/gardenClient/list', {
      gardenId: that.data.gardenId,
      search: this.data.search
    }).then(res => {
      if (res.code == 200) {
        res.rows.forEach(ele => ele.clientTag = ele.clientTag.split(','));
        that.setData({
          customList: this.data.customList.concat(res.rows)
        })
        console.log("this.data.customList------------", this.data.customList)
      } else {
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    })
  },

  /**
   * 获取推广日期数据统计
   */
  get_user_team_data: function () {
    let that = this
    let form = {
      gardenId: that.data.gardenId,
      sign: 2,
    }
    if (that.data.tabCurrent == 5) {
      form.type = ''
    } else if (that.data.month) {
      form.month = that.data.month
      form.type = ''
    } else {
      form.type = that.data.tabCurrent
    }
    ajax.get('/wx/appletRecord/sum', form).then(res => {
      if (res.code == 200) {
        that.setData({
          userTeamCount: res.data
        })
      }
    })
  },

  /**
   * 打电话 联系
   */
  callPhone: function (e) {
    if(!e.currentTarget.id) return ajax.showToast('没有电话号码')
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      gardenId: options.id
    })
    this.get_custom_list()
    this.get_user_team_data(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})